package com.joker.springboottestdemo.job;

import com.joker.demo.core.annotation.JobHandle;
import com.joker.demo.core.domain.dto.JobExecutorParamsDTO;
import com.joker.demo.core.domain.dto.JobExecutorResultDTO;
import com.joker.demo.core.executor.JobExecutorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 第三种类型：类级别 - 实现JobExecutorService接口
 * 支持分片参数
 *
 * @author : feixiang.li
 * @since : 2025-01-27 17:00
 */
@JobHandle(value = "testJobClassLevel",
        cron = "0/16 * * * * ?",
        desc = "类级别任务testJobClassLevel",
        alarmEmail = "feixiang.li@joker.com",
        timeout = 10)
@Service
@Slf4j
public class JobTypeTestClassService implements JobExecutorService {

    @Override
    public JobExecutorResultDTO execute(JobExecutorParamsDTO jobExecutorParamsDTO) {
        log.info("执行类级别任务开始，参数: {}", jobExecutorParamsDTO);

        try {
            for (int i = 0; i < 3; i++) {
                log.info("类级别任务执行中：{}，分片参数: {}, 分片索引: {}",
                        i, jobExecutorParamsDTO.getShardingParam(), jobExecutorParamsDTO.getShardingParamIndex());
                TimeUnit.SECONDS.sleep(1);
            }

            String result = "类级别任务执行完成，分片参数: " + jobExecutorParamsDTO.getShardingParam();
            log.info(result);
            return new JobExecutorResultDTO(result);

        } catch (Exception e) {
            log.error("类级别任务执行异常", e);
            return new JobExecutorResultDTO("执行失败: " + e.getMessage());
        }
    }
} 